package cn.test;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import cn.dao.impl.UserDao;
import cn.entity.User;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class testDao {
	@Test
	public void t1() throws Exception{
		ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"spring.xml"});
		ComboPooledDataSource c3p0 = (ComboPooledDataSource) ac.getBean("comboPooledDataSourceId");
		
	}
	@Test
	public void t2() throws Exception{
		ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"spring.xml"});
		ComboPooledDataSource c3p0 = (ComboPooledDataSource) ac.getBean("comboPooledDataSourceId");
		if(c3p0!=null){System.out.println(c3p0);}
		else {
			System.out.println("失败");
		}
		NamedParameterJdbcTemplate namedjdbctemp = new NamedParameterJdbcTemplate(c3p0);
		String sql="select count(*) from user";
		//可以通过NamedParameterJdbcTemplate得到JdbcTemplate
		int m=namedjdbctemp.getJdbcOperations().queryForInt(sql);
		System.out.println("总数"+m);
	}
	@Test
	public void t3() throws Exception{
		UserDao userDao = new UserDao();
		User user = new User();
		user.setU_id(1);
		User user2 = userDao.findOne(user);
		System.out.println(user2.getU_name());
	}
	@Test
	public void t4() throws Exception{
		UserDao userDao = new UserDao();
		User user = new User();
		user.setU_name("新人5");
		user.setU_remarks("5");
		user.setU_state(1);
		user.setU_createtime(new Date());
		user.setU_lastonline(new Date());
		user.setU_type(2);
		userDao.add(user);
	}
	@Test
	public void t5() throws Exception{
		UserDao userDao = new UserDao();
		List<User> user2 = userDao.findAllRecord(0, 4);
		for (User user : user2) {
			System.out.println(user.getU_id()+"---"+user.getU_name());
		}
	}
	@Test
	public void t6() throws Exception{
		UserDao userDao = new UserDao();
		User user = new User();
		user.setU_id(2);
		User user2 = userDao.findOne(user);
			System.out.println(user2.getU_id()+"---"+user2.getU_name());
	}
	@Test
	public void t7() throws Exception{
		UserDao userDao = new UserDao();
		User user1 = new User();
		user1.setU_id(2);
		User user = userDao.findOne(user1);
		System.out.println(user.getU_id()+"---"+user.getU_name());
		user.setU_name("新人2");
		user.setU_remarks("2");
		user.setU_state(1);
		user.setU_createtime(new Date());
		user.setU_lastonline(new Date());
		user.setU_type(2);
		userDao.update(user);
		System.out.println(user.getU_id()+"---"+user.getU_name());
	}
	@Test
	public void t8() throws Exception{
		 UserDao userDao = new UserDao();
		 int allRecord = userDao.getAllRecord(null);
		 System.out.println(allRecord);
	}
	
	@Test
	public void t9() throws Exception{
		UserDao userDao = new UserDao();
		User user = new User();
		user.setU_id(1);
		user.setU_name("   ");
		user.setU_type(1);
		List<User> findByCondition = userDao.findByCondition(0, 4, user);
		for (User user2 : findByCondition) {
			System.out.println(user2.getU_id()+"---"+user2.getU_name());
		}
		
	}
	
	@Test
	public void t10() throws Exception{
		UserDao userDao = new UserDao();
		User user = new User();
		user.setU_type(1);
		
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		String string = sdf.format(new Date());
		
		
		List<User> findByCondition = userDao.findByCreateTime(0, 4,"2017-02-01 23:06:26","2017-02-07 23:16:27");
		for (User user2 : findByCondition) {
			System.out.println(user2.getU_id()+"---"+user2.getU_name());
		}
	}
	
}
